package com.example.nft.controller;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DemoService {

    public static void main(String[] args) {
        JSONObject jsonObject = new JSONObject ();
        List<String> videohtmls;
        String url = "https://www.338e.net/";
//        String url = "https://www.338e.net/html/202204/45710.html";
        List<String> htmls = getHtmls (url);
//        for (String s : htmls) {
//            if (s.contains (":void(0)")){
//                continue;
//            }
//            videohtmls = getVideoHtml (s);
//            for (String videohtml : videohtmls) {
//                if (s.contains (":void(0)")){
//                    continue;
//                }
//        JSONObject video = getVideo (url, jsonObject);
////                System.out.println (video1);
////            }
////        }
//        System.out.println (video.toString ());
    }
    public static String getHtml(String url){
        String html = "";
        try {
            html = Jsoup.connect(url).execute().body();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return html;
    }


    public static JSONObject getVideo(String html, JSONObject jsonObject){
        String html1 = getHtml(html);
        Document document = Jsoup.parse(html1);
        Elements wrap = document.select (".download");
        String title = document.getElementsByTag("title").get(0).text();
        Elements select = document.select (".pannel.clearfix");
        String data  =  select.select (".data").get (1).text ().replace ("时间：","");
        String fenqu = select.select (".data").get (2).text().replace ("分类：","");


        for (Element element : wrap) {
            Elements a = element.select ("a");
            for (Element element1 : a) {
                String attr = element1.select ("a").attr ("href");
                jsonObject.put ("title",title);
                jsonObject.put ("address",attr);
                jsonObject.put ("datetime",data);
                jsonObject.put ("fenqu",fenqu);
            }
        }
        return jsonObject;
    }

    public static Integer getindex(String html){
        String html1 = getHtml(html);
        String s ="https://www.338e.net";
        Document document = Jsoup.parse(html1);
        Elements a1 = document.select(".moble_pagination").select("a");
        String index =s+a1.get(a1.size() - 1).attr("href");
        String index_ = index.replace(html, "").replace("index_", "").replace(".html", "");
        return Integer.valueOf(index_);
    }

    public static List<String> getVideoHtml(String html){
        Integer getindex = getindex(html);
        List<String> list = new ArrayList<> ();

        for (int i = 1; i<=getindex; i++){
            StringBuilder htmlBuilder = new StringBuilder(html);
            htmlBuilder.append("index_").append(i).append(".html");
            String s = htmlBuilder.toString();
            s = getHtml(s);
            Document document = Jsoup.parse(s);
            Elements wrap = document.select (".preview-item");
            for (Element element : wrap) {
                Elements a = element.select ("a");
                for (Element element1 : a) {
                    String attr = element1.select ("a").attr ("href");
                    list.add ("https://www.338e.net"+attr);
                }
            }
        }
        return list;

    }

    public static List<String> getHtmls(String html){
        html = getHtml(html);
        List<String> list = new ArrayList<> ();
        String s =null;
        Document document = Jsoup.parse(html);
        Elements wrap = document.select (".wrap");
        for (Element element : wrap) {
            Elements a = element.select ("a");
            for (Element element1 : a) {
                String attr = element1.select ("a").attr ("href");
                list.add ("https://www.338e.net"+attr);
            }
        }
        return list;
    }
}
